home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / lightsout.swf / scripts / frame_3 / DoAction.as
Text File  |  2007-10-01  |  15KB  |  533 lines

  1. function buildCube(cubeToBuild)
  2. {
  3.    layersY = [];
  4.    _root.attachMovie("empty","cube",1);
  5.    layer = 0;
  6.    while(layer < cubeToBuild.length)
  7.    {
  8.       cube.attachMovie("empty","layer_" + layer,layer);
  9.       layerWidth = cubeToBuild[layer].length;
  10.       layerHeight = cubeToBuild[layer][0].length;
  11.       game.clip = cube["layer_" + layer];
  12.       game.clip._y = (- layer) * game.tileW - (400 + layer * 2500);
  13.       layersY.push((- layer) * game.tileW);
  14.       x = 0;
  15.       while(x < layerWidth)
  16.       {
  17.          y = 0;
  18.          while(y < layerWidth)
  19.          {
  20.             name = "t_" + x + "_" + y;
  21.             frame = cubeToBuild[layer][x][y];
  22.             depth = (y + x) * game.tileW / 2 * 300 + (y - x) * game.tileW + 1;
  23.             game.clip.attachMovie("tile",name,depth);
  24.             game.clip[name]._x = (y - x) * game.tileW;
  25.             game.clip[name]._y = (y + x) * game.tileW / 2;
  26.             game.clip[name].gotoAndStop(frame);
  27.             game.clip[name].select.gotoAndStop("off");
  28.             y++;
  29.          }
  30.          x++;
  31.       }
  32.       layer++;
  33.    }
  34.    cube._x = Stage.width / 2;
  35.    cube._y = Stage.height / 2;
  36.    game.active = true;
  37. }
  38. function detectKeys()
  39. {
  40.    if(Key.isDown(17))
  41.    {
  42.       game.currentLayer = 0;
  43.       moveLayers();
  44.    }
  45.    else if(Key.isDown(16))
  46.    {
  47.       game.currentLayer = 1;
  48.       moveLayers();
  49.    }
  50.    else
  51.    {
  52.       game.currentLayer = 2;
  53.    }
  54. }
  55. function makeMove()
  56. {
  57.    x = Number(game.currentTile.substr(24,1));
  58.    y = Number(game.currentTile.substr(26,1));
  59.    l = Number(game.currentTile.substr(20,1));
  60.    if(game.Mode == "makeCustom")
  61.    {
  62.       tile = cube["layer_" + l]["t_" + x + "_" + y];
  63.       frame = tile._currentframe;
  64.       if(frame == 1)
  65.       {
  66.          tile.gotoAndStop("on");
  67.       }
  68.       else
  69.       {
  70.          tile.gotoAndStop("off");
  71.       }
  72.    }
  73.    else
  74.    {
  75.       if(game.Mode != "random")
  76.       {
  77.          light_sound.start();
  78.       }
  79.       tile1 = cube["layer_" + l]["t_" + x + "_" + y];
  80.       tile2 = cube["layer_" + l]["t_" + (x + 1) + "_" + y];
  81.       tile3 = cube["layer_" + l]["t_" + (x - 1) + "_" + y];
  82.       tile4 = cube["layer_" + l]["t_" + x + "_" + (y + 1)];
  83.       tile5 = cube["layer_" + l]["t_" + x + "_" + (y - 1)];
  84.       tile6 = cube["layer_" + (l + 1)]["t_" + x + "_" + y];
  85.       tile7 = cube["layer_" + (l - 1)]["t_" + x + "_" + y];
  86.       i = 1;
  87.       while(i <= 7)
  88.       {
  89.          tile = _root["tile" + i];
  90.          if(tile != undefined)
  91.          {
  92.             frame = tile._currentframe;
  93.             if(frame == 1)
  94.             {
  95.                tile.gotoAndStop("on");
  96.             }
  97.             else
  98.             {
  99.                tile.gotoAndStop("off");
  100.             }
  101.          }
  102.          i++;
  103.       }
  104.       if(game.Mode != "random")
  105.       {
  106.          game.totalMoves = game.totalMoves + 1;
  107.          info.moves.text = game.totalMoves;
  108.       }
  109.       if(checkForWin() && game.Mode != "random")
  110.       {
  111.          Quit._y -= 100;
  112.          Reset._y -= 100;
  113.          info._y -= 100;
  114.          removeMovieClip(cube);
  115.          _root.attachMovie("win_page","winPage",1000);
  116.          winPage._x = Stage.width / 2;
  117.          winPage._y = Stage.height / 2;
  118.          game.currentScore = 300;
  119.          if(game.Mode == "normal")
  120.          {
  121.             winPage.gotoAndStop(1);
  122.             winPage.score.text = game.currentScore;
  123.             winPage.moves.text = game.totalMoves;
  124.             winPage.total.text = game.totalScore;
  125.             pauseT = 50;
  126.             scoreTicker = setInterval(calcScore,10);
  127.          }
  128.          else if(game.Mode == "playCustom")
  129.          {
  130.             winPage.gotoAndStop(2);
  131.             winPage.moves.text = game.totalMoves;
  132.             winPage.btn.onRelease = function()
  133.             {
  134.                Quit.btn.onRelease();
  135.             };
  136.          }
  137.          else if(game.Mode == "playRandom")
  138.          {
  139.             winPage.gotoAndStop(2);
  140.             winPage.moves.text = game.totalMoves;
  141.             winPage.btn.onRelease = function()
  142.             {
  143.                Quit.btn.onRelease();
  144.             };
  145.          }
  146.       }
  147.    }
  148. }
  149. function calcScore()
  150. {
  151.    game.overallMoves += game.totalMoves;
  152.    if(pauseT > 0)
  153.    {
  154.       pauseT--;
  155.    }
  156.    else
  157.    {
  158.       if(game.currentScore > 0)
  159.       {
  160.          game.currentScore--;
  161.          game.totalScore = game.totalScore + 1;
  162.          winPage.score.text = game.currentScore;
  163.          winPage.total.text = game.totalScore;
  164.       }
  165.       else if(game.totalMoves > 0)
  166.       {
  167.          game.totalMoves--;
  168.          game.totalScore -= 5;
  169.          winPage.moves.text = game.totalMoves;
  170.          winPage.total.text = game.totalScore;
  171.       }
  172.       winPage.btn.onRelease = function()
  173.       {
  174.          game.totalScore += game.currentScore;
  175.          game.totalMoves = 0;
  176.          game.currentScore = 0;
  177.          info.moves.text = game.totalMoves;
  178.          clearInterval(scoreTicker);
  179.          game.currentMap = game.currentMap + 1;
  180.          if(_root["cubeMap" + game.currentMap] != undefined)
  181.          {
  182.             Quit._y += 100;
  183.             Reset._y += 100;
  184.             info._y += 100;
  185.             saveFile.data.currentMap = game.currentMap;
  186.             saveFile.data.totalScore = game.totalScore;
  187.             saveFile.flush();
  188.             buildCube(_root["cubeMap" + game.currentMap]);
  189.             removeMovieClip(winPage);
  190.             info.lvl.text = game.currentMap;
  191.             info.score.text = game.totalScore;
  192.          }
  193.          else
  194.          {
  195.             winPage.gotoAndStop(3);
  196.             winPage.score.text = game.totalScore;
  197.             winPage.moves.text = game.overallMoves;
  198.             winPage.go_btn.onRelease = function()
  199.             {
  200.                removeMovieClip(cube);
  201.                removeMovieClip(winPage);
  202.                removeMovieClip(Quit);
  203.                _root.gotoAndStop("menu");
  204.             };
  205.             winPage.submit_btn.onRelease = function()
  206.             {
  207.                sendScore();
  208.                winPage.go_btn.onRelease();
  209.             };
  210.          }
  211.       };
  212.    }
  213. }
  214. function checkForWin()
  215. {
  216.    if(game.Mode == "normal" || game.Mode == "playRandom")
  217.    {
  218.       l = 0;
  219.       while(l < _root["cubeMap" + game.currentMap].length)
  220.       {
  221.          x = 0;
  222.          while(x < layerWidth)
  223.          {
  224.             y = 0;
  225.             while(y < layerWidth)
  226.             {
  227.                name = "t_" + x + "_" + y;
  228.                tile = cube["layer_" + l]["t_" + x + "_" + y];
  229.                if(tile._currentframe == 2)
  230.                {
  231.                   return false;
  232.                }
  233.                y++;
  234.             }
  235.             x++;
  236.          }
  237.          l++;
  238.       }
  239.       return true;
  240.    }
  241.    l = 0;
  242.    while(l < game.currentMap.length)
  243.    {
  244.       x = 0;
  245.       while(x < layerWidth)
  246.       {
  247.          y = 0;
  248.          while(y < layerWidth)
  249.          {
  250.             name = "t_" + x + "_" + y;
  251.             tile = cube["layer_" + l]["t_" + x + "_" + y];
  252.             if(tile._currentframe == 2)
  253.             {
  254.                return false;
  255.             }
  256.             y++;
  257.          }
  258.          x++;
  259.       }
  260.       l++;
  261.    }
  262.    return true;
  263. }
  264. function getCustomMap()
  265. {
  266.    tempMap = [];
  267.    tempLayer = [];
  268.    l = 0;
  269.    while(l < 3)
  270.    {
  271.       x = 0;
  272.       while(x < 3)
  273.       {
  274.          tempColl = [];
  275.          y = 0;
  276.          while(y < 3)
  277.          {
  278.             tile = cube["layer_" + l]["t_" + x + "_" + y];
  279.             tempColl.push(tile._currentframe);
  280.             y++;
  281.          }
  282.          tempLayer.push(tempColl);
  283.          x++;
  284.       }
  285.       tempRow = [];
  286.       tempMap.push(tempLayer);
  287.       tempLayer = [];
  288.       l++;
  289.    }
  290.    return tempMap;
  291. }
  292. function saveCustomMap(mapNumber, mapName)
  293. {
  294.    errorMessage.display_txt.text = "*Scanning current map*";
  295.    mapArray = getCustomMap();
  296.    errorMessage.display_txt.text = "*saving out map data...*";
  297.    _root["customMap" + mapNumber] = new Object();
  298.    _root["customMap" + mapNumber].name = mapName;
  299.    _root["customMap" + mapNumber].map = mapArray;
  300.    saveFile.data["customMapName" + mapNumber] = name;
  301.    saveFile.data["customMapArray" + mapNumber] = _root["customMap" + mapNumber].map;
  302.    errorMessage.display_txt.text = "*Map Data saved successfully*";
  303.    errorMessage.play();
  304.    saveFile.data["customMapName" + mapNumber] = mapName;
  305.    saveFile.data["customMapArray" + mapNumber] = _root["customMap" + mapNumber].map;
  306.    saveFile.flush();
  307.    savePage.cancel_btn.onRelease();
  308. }
  309. function sendScore()
  310. {
  311.    var _loc3_ = "http://localhost/gameTest2/lightOut_highscores.php?task=submit&user=" + winPage.user.text + "&score=" + game.totalScore;
  312.    var _loc2_ = new LoadVars();
  313.    _loc2_.onLoad = function(success)
  314.    {
  315.       if(success)
  316.       {
  317.          removeMovieClip(cube);
  318.          removeMovieClip(winPage);
  319.          removeMovieClip(Quit);
  320.          _root.gotoAndStop("menu");
  321.       }
  322.       else
  323.       {
  324.          errorMessage.display_txt.text = "*Error occured when sending score*";
  325.          errorMessage.play();
  326.       }
  327.    };
  328. }
  329. function randomMoves()
  330. {
  331.    moveTotal = Math.ceil(Math.random() * 10) + 6;
  332.    m = 0;
  333.    while(m < moveTotal)
  334.    {
  335.       ranX = Math.ceil(Math.random() * 2);
  336.       ranY = Math.ceil(Math.random() * 2);
  337.       ranZ = Math.ceil(Math.random() * 2);
  338.       tile = _level0.cube["layer_" + ranZ]["t_" + ranX + "_" + ranY];
  339.       game.currentTile = tile;
  340.       makeMove();
  341.       m++;
  342.    }
  343.    randomArray = getCustomMap();
  344.    game.Mode = "playRandom";
  345. }
  346. cubeMap0 = [[[1,1,1],[1,1,1],[1,1,1]],[[1,1,1],[1,1,1],[1,1,1]],[[1,1,1],[1,1,1],[1,1,1]]];
  347. cubeMap1 = [[[2,1,2],[1,1,1],[2,1,2]],[[1,1,1],[1,1,1],[1,1,1]],[[2,1,2],[1,1,1],[2,1,2]]];
  348. cubeMap2 = [[[2,1,1],[2,2,2],[1,1,2]],[[1,1,2],[1,2,1],[2,1,1]],[[2,1,1],[2,2,2],[1,1,2]]];
  349. cubeMap3 = [[[1,1,1],[1,2,1],[1,1,2]],[[1,1,2],[1,2,2],[2,2,2]],[[1,2,1],[2,2,1],[1,1,2]]];
  350. cubeMap4 = [[[1,1,1],[1,1,1],[1,1,1]],[[1,1,2],[1,1,1],[2,1,2]],[[1,1,1],[1,1,1],[1,1,1]]];
  351. cubeMap5 = [[[1,1,1],[1,1,1],[1,1,1]],[[1,2,1],[2,1,2],[1,2,1]],[[1,1,1],[1,1,1],[1,1,1]]];
  352. cubeMap6 = [[[2,2,2],[2,1,2],[2,2,2]],[[2,2,2],[2,1,2],[2,2,2]],[[2,2,2],[2,1,2],[2,2,2]]];
  353. cubeMap7 = [[[1,1,1],[2,1,1],[1,1,1]],[[1,1,1],[1,2,1],[1,1,1]],[[1,1,1],[1,1,2],[1,1,1]]];
  354. cubeMap8 = [[[1,1,2],[1,1,2],[2,2,1]],[[1,2,1],[2,1,1],[1,1,1]],[[1,1,2],[1,1,2],[2,2,1]]];
  355. cubeMap9 = [[[2,2,2],[2,2,2],[2,2,2]],[[1,1,1],[1,2,1],[1,1,1]],[[2,2,2],[2,2,2],[2,2,2]]];
  356. cubeMap10 = [[[1,1,1],[1,1,1],[1,1,1]],[[1,1,1],[1,2,1],[1,1,1]],[[1,1,1],[1,1,1],[1,1,1]]];
  357. cubeMap11 = [[[2,1,2],[1,2,1],[2,1,2]],[[1,2,1],[2,2,2],[1,2,1]],[[2,1,2],[1,2,1],[2,1,2]]];
  358. cubeMap12 = [[[2,1,1],[1,1,1],[1,1,2]],[[2,2,2],[2,1,2],[2,2,2]],[[1,1,2],[1,1,1],[2,1,1]]];
  359. cubeMap13 = [[[2,1,2],[1,1,1],[2,1,2]],[[2,1,1],[1,2,1],[1,1,2]],[[2,1,2],[1,1,1],[2,1,2]]];
  360. cubeMap14 = [[[2,2,2],[2,2,2],[2,2,2]],[[2,1,2],[2,1,2],[2,1,2]],[[2,2,2],[2,2,2],[2,2,2]]];
  361. cubeMap15 = [[[2,1,1],[1,1,1],[2,1,2]],[[1,2,2],[2,1,2],[2,2,1]],[[2,1,2],[1,1,1],[1,1,2]]];
  362. cubeMap16 = [[[2,1,2],[1,1,1],[2,1,2]],[[2,1,2],[1,1,1],[2,1,2]],[[1,1,1],[2,2,2],[1,1,1]]];
  363. if(game.Mode == "playCustom")
  364. {
  365.    buildCube(game.currentMap);
  366. }
  367. else if(game.Mode == "normal")
  368. {
  369.    buildCube(_root["cubeMap" + game.currentMap]);
  370. }
  371. else if(game.Mode == "makeCustom")
  372. {
  373.    buildCube(cubeMap0);
  374. }
  375. else if(game.Mode == "random")
  376. {
  377.    buildCube(cubeMap0);
  378.    randomMoves();
  379. }
  380. onEnterFrame = function()
  381. {
  382.    detectKeys();
  383.    if(game.active)
  384.    {
  385.       dy3 = cube.layer_0._y - layersY[0];
  386.       if(dy3 < 0)
  387.       {
  388.          cube.layer_0._y -= dy3 * 0.1;
  389.       }
  390.       else
  391.       {
  392.          cube.layer_0._y = layersY[0];
  393.       }
  394.       if(game.currentLayer == 0)
  395.       {
  396.          dy = cube.layer_2._y - (layersY[2] - 120);
  397.          if(dy > 0)
  398.          {
  399.             cube.layer_2._y -= dy * 0.1;
  400.          }
  401.          else
  402.          {
  403.             cube.layer_2._y = layersY[2] - 120;
  404.          }
  405.          dy2 = cube.layer_1._y - (layersY[1] - 120);
  406.          if(dy2 > 0)
  407.          {
  408.             cube.layer_1._y -= dy2 * 0.1;
  409.          }
  410.          else
  411.          {
  412.             cube.layer_1._y = layersY[1] - 120;
  413.          }
  414.       }
  415.       else if(game.currentLayer == 1)
  416.       {
  417.          dy = cube.layer_2._y - (layersY[2] - 120);
  418.          if(dy > 0)
  419.          {
  420.             cube.layer_2._y -= dy * 0.1;
  421.          }
  422.          else
  423.          {
  424.             cube.layer_2._y = layersY[2] - 120;
  425.          }
  426.          dy2 = cube.layer_1._y - layersY[1];
  427.          if(dy2 < 0)
  428.          {
  429.             cube.layer_1._y -= dy2 * 0.1;
  430.          }
  431.          else
  432.          {
  433.             cube.layer_1._y = layersY[1];
  434.          }
  435.       }
  436.       else if(game.currentLayer == 2)
  437.       {
  438.          dy = cube.layer_2._y - layersY[2];
  439.          if(dy < 0)
  440.          {
  441.             cube.layer_2._y -= dy * 0.1;
  442.          }
  443.          else
  444.          {
  445.             cube.layer_2._y = layersY[2];
  446.          }
  447.          dy2 = cube.layer_1._y - layersY[1];
  448.          if(dy2 < 0)
  449.          {
  450.             cube.layer_1._y -= dy2 * 0.1;
  451.          }
  452.          else
  453.          {
  454.             cube.layer_1._y = layersY[1];
  455.          }
  456.       }
  457.    }
  458. };
  459. saveCustom.btn.onRelease = function()
  460. {
  461.    _root.attachMovie("save_page","savePage",100);
  462.    savePage._x = Stage.width / 2;
  463.    savePage._y = Stage.height / 2;
  464.    saveCustom._y -= 100;
  465.    i = 1;
  466.    while(i < 6)
  467.    {
  468.       list = savePage["name" + i];
  469.       if(_root["customMap" + i].name != undefined)
  470.       {
  471.          list.text = _root["customMap" + i].name;
  472.       }
  473.       i++;
  474.    }
  475.    savePage.btn1.onRelease = function()
  476.    {
  477.       saveCustomMap(1,savePage.name1.text);
  478.    };
  479.    savePage.btn2.onRelease = function()
  480.    {
  481.       saveCustomMap(2,savePage.name2.text);
  482.    };
  483.    savePage.btn3.onRelease = function()
  484.    {
  485.       saveCustomMap(3,savePage.name3.text);
  486.    };
  487.    savePage.btn4.onRelease = function()
  488.    {
  489.       saveCustomMap(4,savePage.name4.text);
  490.    };
  491.    savePage.btn5.onRelease = function()
  492.    {
  493.       saveCustomMap(5,savePage.name5.text);
  494.    };
  495.    savePage.cancel_btn.onRelease = function()
  496.    {
  497.       removeMovieClip(savePage);
  498.       saveCustom._y += 100;
  499.    };
  500. };
  501. Quit.btn.onRelease = function()
  502. {
  503.    removeMovieClip(cube);
  504.    removeMovieClip(Quit);
  505.    removeMovieClip(Reset);
  506.    removeMovieClip(info);
  507.    removeMovieClip(saveCustom);
  508.    removeMovieClip(savePage);
  509.    removeMovieClip(winPage);
  510.    gotoAndStop("menu");
  511. };
  512. Reset.btn.onRelease = function()
  513. {
  514.    game.totalMoves = 0;
  515.    info.moves.text = "0";
  516.    if(game.Mode == "playCustom")
  517.    {
  518.       buildCube(game.currentMap);
  519.    }
  520.    else if(game.Mode == "normal")
  521.    {
  522.       buildCube(_root["cubeMap" + game.currentMap]);
  523.    }
  524.    else if(game.Mode == "makeCustom")
  525.    {
  526.       buildCube(cubeMap0);
  527.    }
  528.    else if(game.Mode == "playRandom")
  529.    {
  530.       buildCube(randomArray);
  531.    }
  532. };
  533.